"""
Case Type   : cm_uninstall
Case Name   : cm_uninstall执行前，集群各节点的互信有问题
Create At   : 2025/4/24
Owner       : ningyali
Description :
    1.删除数据库节点间的互信
    2.取消配置xml文件中的cm相关参数
    3.用户下执行cm_uninstall
    4.cm_ctl查看集群状态
    5.gs_om查看集群状态
    6.sshfile文件中放置要互信的节点ip,创建互信
    7.清理环境
Expect      :
    1.执行成功
    2.执行成功
    3.卸载失败
    4.集群状态正常，cm未卸载
    5.状态正常，cm未卸载
    6.执行成功
    7.执行成功
History     :
"""
import os
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node_names = (
            'PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
        self.sh = [CommonSH(n) for n in self.user_node_names]
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = [Node(n) for n in self.user_node_names]
        self.common = Common()
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.sh[0].node.ssh_user)
        self.un_cm = CmUnInstall(self.install_pkg_path,
                                 self.sh[0].node.ssh_user)

        text = '----prestep1:安装cm;expect:执行成功----'
        self.log.info(text)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')

        res = self.cm.cm_install(self.user_nodes[0])
        self.assertIn('Install CM tool success', res, f'执行失败: {text}')

    def test_cm_uninstall(self):
        text = '----step1:删除数据库节点间的互信;expect:执行成功----'
        self.log.info(text)
        rm_cmd = f"rm -rf ~/.ssh"
        rm_res = self.common.get_sh_result(self.user_nodes[0], rm_cmd)
        self.assertNotIn('error', rm_res.lower(), f'执行失败: {text}')

        text = '----step2:取消配置xml文件中的cm相关参数 expect:执行成功----'
        self.log.info(text)
        sed_cmd = f'sed -i \'/cm/d\' {self.cm.xml_path};'
        sed_res = self.common.get_sh_result(self.user_nodes[0], sed_cmd)
        self.assertNotIn('error', sed_res.lower(), "执行失败:" + text)

        text = '----step3:用户下执行cm_uninstall expect:卸载失败----'
        self.log.info(text)
        res = self.un_cm.cm_uninstall(
            self.user_nodes[0],
            f'-e {macro.DB_ENV_PATH} --deleteBinary --deleteData')
        expect = f"[GAUSS-51100] : Failed to verify SSH trust on these nodes"
        self.assertIn(expect, res, f'执行失败: {text}')

        text = '----step4:cm_ctl查看集群状态;expect:集群状态正常，cm未卸载----'
        self.log.info(text)
        show_status = f'source {macro.DB_ENV_PATH}; cm_ctl query -Civdp'
        status_msg = self.common.get_sh_result(self.user_nodes[0], show_status)
        self.assertIn("CMServer State", status_msg, "执行失败:" + text)

        text = '----step5:gs_om查看集群状态;expect:状态正常，cm未卸载----'
        self.log.info(text)
        status_msg = self.sh[0].get_db_cluster_status('detail')
        self.log.info(status_msg)
        self.assertIn("CMServer State", status_msg, "执行失败:" + text)

    def tearDown(self):
        text = '----step6:sshfile文件中放置要互信的节点ip,创建互信;expect:执行成功----'
        self.log.info(text)
        create_cmd = f'source {macro.DB_ENV_PATH};' \
                     f'echo -e "{self.user_nodes[0].ssh_host}\n' \
                     f'{self.user_nodes[1].ssh_host}\n' \
                     f'{self.user_nodes[2].ssh_host}\n" >> tmp_hosts;' \
                     f'echo {self.user_nodes[0].ssh_password}|' \
                     f'gs_sshexkey -f tmp_hosts;' \
                     f'rm -rf tmp_hosts'
        create_res = self.common.get_sh_result(self.user_nodes[0], create_cmd)

        text = '----step7:清理环境 expect:执行成功----'
        self.log.info(text)
        res = self.un_cm.cm_uninstall(self.user_nodes[0],
                                      '--deleteBinary --deleteData')
        self.assertIn(
            "Successfully created SSH trust", create_res, "执行失败:" + text)
        self.assertIn('Uninstall CM tool success', res, f'执行失败: {text}')
        self.log.info(f'----{os.path.basename(__file__)} end----')
